Skip to content

feat!: Change Pandas timestamp resolution to ms for timestamp attributes#2485

Open
MortGron wants to merge 5 commits intopysdk-release-v8from
pandas-timestamp-resolution
Open

feat!: Change Pandas timestamp resolution to ms for timestamp attributes#2485
MortGron wants to merge 5 commits intopysdk-release-v8from
pandas-timestamp-resolution

Conversation

@MortGron
Copy link
Contributor

@MortGron MortGron commented Feb 22, 2026

Description

Currently when using to_pandas on list like objects a timestamp resolution of 'ns' is enforced for both Pandas v2 and v3. For single Cognite resources that have only timestamp attributes, resolution will be 'ns' in Pandas v2 and 'ms' in v3. For single Cognite resources that have at least on timestamp attribute and at least one non-timestamp attribute, time resolution will be 'ms' in both v2 and v3.

In Pandas v3 the timestamp resolution is to a much larger extent inferred from the input data, where Pandas v2 would automatically set resolution 'ns'. CDF has a resolution of 'ms', so it would make sense for the to_pandas methods to also convert timestamp columns to ms.

An example where keeping resolution 'ns' would be inconenient in Pandas v3 is when using the from pandas.testing import assert_frame_equal where the resolution of timestamp columns are compared.

Checklist:

  • Tests added/updated.
  • Documentation updated. Documentation is generated from docstrings - these must be updated according to your change.
    If a new method has been added it should be referenced in cognite.rst in order to generate docs based on its docstring.
  • The PR title follows the Conventional Commit spec.

@MortGron MortGron changed the title feat!: Change Pandas timestamp resolution to ms for timestamp columns feat!: Change Pandas timestamp resolution to ms for timestamp attributes Feb 24, 2026
@MortGron MortGron marked this pull request as ready for review February 24, 2026 19:49
@MortGron MortGron requested review from a team as code owners February 24, 2026 19:49
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @MortGron, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request standardizes the Pandas timestamp resolution within the SDK's to_pandas methods to milliseconds. This change addresses inconsistencies that arose with Pandas v3's improved timestamp inference, ensuring that all timestamp attributes are uniformly represented as datetime64[ms]. The update improves data consistency when interacting with CDF's millisecond-based timestamps and prevents potential issues with timestamp comparison functions in Pandas.

Highlights

  • Standardized Pandas Timestamp Resolution: The to_pandas methods and timestamp conversion utilities now consistently use millisecond resolution (datetime64[ms]) for timestamp attributes, aligning with CDF's native resolution.
  • Explicit Millisecond Conversion in to_pandas: The to_pandas method in _base.py was updated to explicitly cast the 'value' column to datetime64[ms] when a resource consists solely of timestamp attributes.
  • Simplified Timestamp Column Conversion: The convert_timestamp_columns_to_datetime helper function now directly converts timestamp columns to datetime64[ms], removing a previous intermediate nanosecond conversion step.
  • Updated Unit Tests: Existing unit tests were adjusted to reflect the new millisecond timestamp resolution, and a new test case was added to specifically validate the to_pandas behavior for resources containing only timestamp attributes.
  • Dependency Update: The poetry.lock file was updated, primarily reflecting an upgrade of the virtualenv dependency.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • cognite/client/data_classes/_base.py
    • Modified to_pandas to explicitly cast value column to datetime64[ms] if all dumped keys are time attributes.
  • cognite/client/utils/_pandas_helpers.py
    • Updated convert_timestamp_columns_to_datetime to convert timestamp columns directly to datetime64[ms].
  • poetry.lock
    • Updated virtualenv dependency from 20.37.0 to 20.39.0.
    • Removed [package.extras] section related to docs and test dependencies.
  • tests/tests_unit/test_base.py
    • Updated SomeResource definition to include a created_time attribute and adjusted the expected DataFrame in test_to_pandas.
    • Added test_to_pandas_time_dtype_if_only_timestamp_attributes to verify datetime64[ms] conversion for resources with only timestamp attributes.
    • Added a comment and explicit astype for lastUpdatedTime in test_to_pandas for TestCogniteResourceList.
  • tests/tests_unit/test_data_classes/test_data_models/test_typed_instances.py
    • Modified test_to_pandas_list to explicitly cast created_time and last_updated_time columns to datetime64[ms] in the expected DataFrame.
  • tests/tests_unit/test_data_classes/test_groups.py
    • Updated test_to_pandas_list_with_timestamps to expect datetime64[ms] dtype for deleted_time.
Activity
  • Tests were added and updated to reflect the changes.
  • The pull request title adheres to the Conventional Commit specification.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request changes the timestamp resolution from nanoseconds to milliseconds in to_pandas methods to align with CDF's native millisecond precision. The changes are implemented correctly in cognite/client/utils/_pandas_helpers.py and cognite/client/data_classes/_base.py. All related tests have been updated to reflect this new behavior, ensuring the change is well-tested. The code modifications are logical and adhere to the project's style guide. I found no high or critical severity issues.

@codecov
Copy link

codecov bot commented Feb 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 64.60%. Comparing base (b268276) to head (25ccff4).
⚠️ Report is 2 commits behind head on pysdk-release-v8.

Additional details and impacted files
@@                Coverage Diff                @@
##           pysdk-release-v8    #2485   +/-   ##
=================================================
  Coverage             64.59%   64.60%           
=================================================
  Files                   467      467           
  Lines                 47789    47808   +19     
=================================================
+ Hits                  30871    30887   +16     
- Misses                16918    16921    +3     
Files with missing lines Coverage Δ
cognite/client/data_classes/_base.py 49.67% <100.00%> (+0.33%) ⬆️
cognite/client/utils/_pandas_helpers.py 60.57% <100.00%> (ø)
tests/tests_unit/test_base.py 98.93% <100.00%> (+0.03%) ⬆️
...a_classes/test_data_models/test_typed_instances.py 99.38% <100.00%> (+0.01%) ⬆️
tests/tests_unit/test_data_classes/test_groups.py 100.00% <ø> (ø)

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant